/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_frct_add.c                                       */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/17 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frlist.h"
#include "frsutl.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_hash.h"
#include "frame_params.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frct_mng.h"
#include "apmtest_cunit.h"
#include "frstat_pkg.h"
#include "frct_expect.h"

extern Frhash_header        *g_p_st_hash_ct;
void __frct_hash_node_free(Frlist_node *p_st_node);
extern struct event_base *g_p_st_ebase;
extern int g_i_flag_run;
extern Frapm_params st_apm_params;
void __frct_free(Conntrack *p_st_ct);

static int __suite_init(void)
{
    frtimer_init();
    frstat_pkg_init();
    frexpect_init(1);

    g_p_st_ebase = event_base_new();
    return 0;
}

static int __suite_cleanup(void)
{
    event_base_free(g_p_st_ebase);
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_ct=NULL;
    char *p_src_mac = NULL;
    char *p_dst_mac = NULL;
    u_short us_proto = IPPROTO_UDP;
    u_int ui_src = iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr((char *)"192.168.0.1");
    u_short us_dport = 22;
    int i_flag_get_ct = FR_YES;
    int i_status = APM_CONNECT_SYN_SENT;
    char *p_data = NULL;
    unsigned int ui_data_len = 4;
    struct timeval st_timeval_pkg;
    char *p_tmp = NULL;
    int i_flag_insert_task = FR_YES;
    Franalyz_cmd em_analyz_cmd = FRANALYZ_CMD_DATA;

    frct_hash_init(5);

    p_tmp = (char *)frmalloc(strlen("6e:80:00:ea:9f:8c") + 1);
    snprintf(p_tmp, strlen("6e:80:00:ea:9f:8c") + 1,"%s","6e:80:00:ea:9f:8c");
    p_src_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_src_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_tmp = (char *)frmalloc(strlen("00:8e:6c:90:fe:a4") + 1);
    snprintf(p_tmp, strlen("00:8e:6c:90:fe:a4") + 1,"00:8e:6c:90:fe:a4");
    p_dst_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_dst_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_data = (char *)frmalloc(ui_data_len + 1);
    snprintf(p_data,ui_data_len + 1,"%s","test");

    st_timeval_pkg.tv_sec = 500;
    st_timeval_pkg.tv_usec = 0;

    p_st_ct = frct_add(p_src_mac, p_dst_mac, us_proto, ui_src, us_sport, ui_dest
                        , us_dport, i_flag_insert_task, em_analyz_cmd, i_flag_get_ct
                        , i_status, p_data, ui_data_len, st_timeval_pkg, 0, 0);

    CU_ASSERT_PTR_NULL(p_st_ct);

    frfree(p_data);
    frfree(p_src_mac);
    frfree(p_dst_mac);
    frct_destroy();

}

static void __case2(void)
{
    Conntrack *p_st_ct=NULL;
    char *p_src_mac = NULL;
    char *p_dst_mac = NULL;
    u_short us_proto = IPPROTO_UDP;
    u_int ui_src = iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr((char *)"192.168.0.1");
    u_short us_dport = 22;
    int i_flag_get_ct = FR_YES;
    int i_status = APM_CONNECT_SYN_SENT;
    char *p_data = NULL;
    char arr_key[15];
    unsigned int ui_data_len = 4;
    struct timeval st_timeval_pkg;
    char *p_tmp = NULL;
    int i_flag_insert_task = FR_YES;
    Franalyz_cmd em_analyz_cmd = FRANALYZ_CMD_DATA;

    frct_hash_init(5);
    st_apm_params.ui_ct_max_cnt = 1000;
    

    p_tmp = (char *)frmalloc(strlen("6e:80:00:ea:9f:8c") + 1);
    snprintf(p_tmp, strlen("6e:80:00:ea:9f:8c") + 1,"%s","6e:80:00:ea:9f:8c");
    p_src_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_src_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_tmp = (char *)frmalloc(strlen("00:8e:6c:90:fe:a4") + 1);
    snprintf(p_tmp, strlen("00:8e:6c:90:fe:a4") + 1,"00:8e:6c:90:fe:a4");
    p_dst_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_dst_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_data = (char *)frmalloc(ui_data_len + 1);
    snprintf(p_data,ui_data_len + 1,"%s","test");

    st_timeval_pkg.tv_sec = 500;
    st_timeval_pkg.tv_usec = 0;

    p_st_ct = frct_add(p_src_mac, p_dst_mac, us_proto, ui_src, us_sport, ui_dest
                        , us_dport, FR_YES, FRANALYZ_CMD_SYN, i_flag_get_ct
                        , APM_CONNECT_SYN_SENT, NULL, 0, st_timeval_pkg, 0, 0);

    p_st_ct = frct_add(p_src_mac, p_dst_mac, us_proto, ui_src, us_sport, ui_dest
                        , us_dport, i_flag_insert_task, em_analyz_cmd,i_flag_get_ct
                        , i_status, p_data, ui_data_len, st_timeval_pkg, 0, 0);

    CU_ASSERT_EQUAL(frlist_size(&p_st_ct->lst_cap_queue), 2);
    CU_ASSERT_EQUAL(p_st_ct->flg_status,APM_CONNECT_SYN_SENT);
    CU_ASSERT_EQUAL(p_st_ct->i_flag_l7proto_status,APM_L7PROTO_STATUS_UNKNOWN);
    CU_ASSERT_EQUAL(p_st_ct->src_ip,iptools_parse_ipaddr((char *)"192.168.1.1"));
    CU_ASSERT_EQUAL(p_st_ct->dst_ip,iptools_parse_ipaddr((char *)"192.168.0.1"));
    CU_ASSERT_STRING_EQUAL(p_st_ct->src_mac,p_src_mac);
    CU_ASSERT_STRING_EQUAL(p_st_ct->dst_mac,p_dst_mac);
    CU_ASSERT_EQUAL(p_st_ct->src_port,80);
    CU_ASSERT_EQUAL(p_st_ct->dst_port,22);
    CU_ASSERT_EQUAL(p_st_ct->st_connection_created.tv_sec,500);
    CU_ASSERT_EQUAL(p_st_ct->st_connection_lastskb_time.tv_sec,500);
    CU_ASSERT_PTR_NOT_NULL(p_st_ct->p_st_timer_entry);


    frfree(p_data);
    frfree(p_src_mac);
    frfree(p_dst_mac);
    frct_destroy();

    return;

}


static void __case3(void)
{
    Conntrack *p_st_ct=NULL;
    char *p_src_mac = NULL;
    char *p_dst_mac = NULL;
    u_short us_proto = IPPROTO_UDP;
    u_int ui_src = iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr((char *)"192.168.0.1");
    u_short us_dport = 22;
    int i_flag_get_ct = FR_NO;
    int i_status = APM_CONNECT_SYN_SENT;
    char *p_data = NULL;
    unsigned int ui_data_len = 4;
    struct timeval st_timeval_pkg;
    char *p_tmp = NULL;
    int i_flag_insert_task = FR_YES;
    Franalyz_cmd em_analyz_cmd = FRANALYZ_CMD_DATA;

    frct_hash_init(5);
    st_apm_params.ui_ct_max_cnt = 1000;
    

    p_tmp = (char *)frmalloc(strlen("6e:80:00:ea:9f:8c") + 1);
    snprintf(p_tmp, strlen("6e:80:00:ea:9f:8c") + 1,"%s","6e:80:00:ea:9f:8c");
    p_src_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_src_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_tmp = (char *)frmalloc(strlen("00:8e:6c:90:fe:a4") + 1);
    snprintf(p_tmp, strlen("00:8e:6c:90:fe:a4") + 1,"00:8e:6c:90:fe:a4");
    p_dst_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_dst_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_data = (char *)frmalloc(ui_data_len + 1);
    snprintf(p_data,ui_data_len + 1,"%s","test");

    st_timeval_pkg.tv_sec = 500;
    st_timeval_pkg.tv_usec = 0;


    p_st_ct = frct_add(p_src_mac, p_dst_mac, us_proto, ui_src, us_sport, ui_dest
                        , us_dport, i_flag_insert_task, em_analyz_cmd,i_flag_get_ct
                        , i_status, p_data, ui_data_len, st_timeval_pkg, 0, 0);

    CU_ASSERT_PTR_NULL(p_st_ct);


    frfree(p_data);
    frfree(p_src_mac);
    frfree(p_dst_mac);
    frct_hash_destory(__frct_hash_node_free);
    frct_destroy();
    return;

}

static void __case4(void)
{
    Conntrack *p_st_ct=NULL;
    char *p_src_mac = NULL;
    char *p_dst_mac = NULL;
    u_short us_proto = IPPROTO_UDP;
    u_int ui_src = iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr((char *)"192.168.0.1");
    u_short us_dport = 22;
    int i_flag_get_ct = FR_YES;
    int i_status = APM_CONNECT_ESTABLISHED;
    char *p_data = NULL;
    unsigned int ui_data_len = 4;
    struct timeval st_timeval_pkg;
    char *p_tmp = NULL;
    int i_flag_insert_task = FR_YES;
    Franalyz_cmd em_analyz_cmd = FRANALYZ_CMD_DATA;

    frct_hash_init(5);
    st_apm_params.ui_ct_max_cnt = 1000;
    

    p_tmp = (char *)frmalloc(strlen("6e:80:00:ea:9f:8c") + 1);
    snprintf(p_tmp, strlen("6e:80:00:ea:9f:8c") + 1,"%s","6e:80:00:ea:9f:8c");
    p_src_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_src_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_tmp = (char *)frmalloc(strlen("00:8e:6c:90:fe:a4") + 1);
    snprintf(p_tmp, strlen("00:8e:6c:90:fe:a4") + 1,"00:8e:6c:90:fe:a4");
    p_dst_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_dst_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_data = (char *)frmalloc(ui_data_len + 1);
    snprintf(p_data,ui_data_len + 1,"%s","test");

    st_timeval_pkg.tv_sec = 500;
    st_timeval_pkg.tv_usec = 0;

    

    p_st_ct = frct_add(p_src_mac, p_dst_mac, us_proto, ui_src, us_sport, ui_dest
                        , us_dport, FR_YES, FRANALYZ_CMD_SYN, i_flag_get_ct
                        , APM_CONNECT_SYN_SENT, NULL, 0, st_timeval_pkg,0 ,0);

    p_st_ct = frct_add(p_src_mac, p_dst_mac, us_proto, ui_src, us_sport, ui_dest
                        , us_dport, i_flag_insert_task, em_analyz_cmd,i_flag_get_ct
                        , i_status, p_data, ui_data_len, st_timeval_pkg, 0, 0);

    p_st_ct = frct_add(p_src_mac, p_dst_mac, us_proto, ui_src, us_sport, ui_dest
                        , us_dport, FR_YES, FRANALYZ_CMD_FIN, i_flag_get_ct
                        , APM_CONNECT_ESTABLISHED, NULL, 0, st_timeval_pkg, 0, 0);

    p_st_ct = frct_add(p_src_mac, p_dst_mac, us_proto, ui_src, us_sport, ui_dest
                        , us_dport, FR_YES, FRANALYZ_CMD_SYN, i_flag_get_ct
                        , APM_CONNECT_SYN_SENT, NULL, 0, st_timeval_pkg, 0, 0);

    CU_ASSERT_EQUAL(frlist_size(&p_st_ct->lst_cap_queue), 4);
    CU_ASSERT_EQUAL(p_st_ct->flg_update,FR_YES);
    CU_ASSERT_EQUAL(p_st_ct->flg_status,APM_CONNECT_SYN_SENT);
    CU_ASSERT_EQUAL(p_st_ct->i_flag_l7proto_status,APM_L7PROTO_STATUS_UNKNOWN);
    CU_ASSERT_EQUAL(p_st_ct->src_ip,iptools_parse_ipaddr((char *)"192.168.1.1"));
    CU_ASSERT_EQUAL(p_st_ct->dst_ip,iptools_parse_ipaddr((char *)"192.168.0.1"));
    CU_ASSERT_STRING_EQUAL(p_st_ct->src_mac,p_src_mac);
    CU_ASSERT_STRING_EQUAL(p_st_ct->dst_mac,p_dst_mac);
    CU_ASSERT_EQUAL(p_st_ct->src_port,80);
    CU_ASSERT_EQUAL(p_st_ct->dst_port,22);
    CU_ASSERT_EQUAL(p_st_ct->st_connection_created.tv_sec,500);
    CU_ASSERT_EQUAL(p_st_ct->st_connection_lastskb_time.tv_sec,500);
    CU_ASSERT_EQUAL(p_st_ct->st_connection_established.tv_sec,500);
    CU_ASSERT_PTR_NOT_NULL(p_st_ct->p_st_timer_entry);

    frfree(p_data);
    frfree(p_src_mac);
    frfree(p_dst_mac);
    frct_hash_destory(__frct_hash_node_free);
    frct_destroy();

    return;

}


static void __case5(void)
{
    Conntrack *p_st_ct=NULL;
    char *p_src_mac = NULL;
    char *p_dst_mac = NULL;
    u_short us_proto = IPPROTO_UDP;
    u_int ui_src = iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr((char *)"192.168.0.1");
    u_short us_dport = 22;
    int i_flag_get_ct = FR_YES;
    int i_status = APM_CONNECT_ESTABLISHED;
    char *p_data = NULL;
    unsigned int ui_data_len = 4;
    struct timeval st_timeval_pkg;
    char *p_tmp = NULL;
    int i_flag_insert_task = FR_YES;
    Franalyz_cmd em_analyz_cmd = FRANALYZ_CMD_DATA;


    frct_hash_init(5);
    st_apm_params.ui_ct_max_cnt = 1000;
    

    p_tmp = (char *)frmalloc(strlen("6e:80:00:ea:9f:8c") + 1);
    snprintf(p_tmp, strlen("6e:80:00:ea:9f:8c") + 1,"%s","6e:80:00:ea:9f:8c");
    p_src_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_src_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_tmp = (char *)frmalloc(strlen("00:8e:6c:90:fe:a4") + 1);
    snprintf(p_tmp, strlen("00:8e:6c:90:fe:a4") + 1,"00:8e:6c:90:fe:a4");
    p_dst_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_dst_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_data = (char *)frmalloc(ui_data_len + 1);
    snprintf(p_data,ui_data_len + 1,"%s","test");

    st_timeval_pkg.tv_sec = 500;
    st_timeval_pkg.tv_usec = 0;

    p_st_ct = frct_add(p_src_mac, p_dst_mac, us_proto, ui_src, us_sport, ui_dest
                        , us_dport, i_flag_insert_task, em_analyz_cmd,i_flag_get_ct
                        , i_status, p_data, ui_data_len, st_timeval_pkg, 0, 0);

    st_timeval_pkg.tv_sec = 1000;

    p_st_ct = frct_add(p_src_mac, p_dst_mac, us_proto, ui_src, us_sport, ui_dest
                        , us_dport, i_flag_insert_task, em_analyz_cmd,i_flag_get_ct
                        , i_status, p_data, ui_data_len, st_timeval_pkg, 0, 0);

    CU_ASSERT_EQUAL(p_st_ct->flg_update,FR_YES);
    CU_ASSERT_EQUAL(p_st_ct->flg_status,APM_CONNECT_ESTABLISHED);
    CU_ASSERT_EQUAL(p_st_ct->i_flag_l7proto_status,APM_L7PROTO_STATUS_UNKNOWN);
    CU_ASSERT_EQUAL(p_st_ct->src_ip,iptools_parse_ipaddr((char *)"192.168.1.1"));
    CU_ASSERT_EQUAL(p_st_ct->dst_ip,iptools_parse_ipaddr((char *)"192.168.0.1"));
    CU_ASSERT_STRING_EQUAL(p_st_ct->src_mac,p_src_mac);
    CU_ASSERT_STRING_EQUAL(p_st_ct->dst_mac,p_dst_mac);
    CU_ASSERT_EQUAL(p_st_ct->src_port,80);
    CU_ASSERT_EQUAL(p_st_ct->dst_port,22);
    CU_ASSERT_EQUAL(p_st_ct->st_connection_created.tv_sec,500);
    CU_ASSERT_EQUAL(p_st_ct->st_connection_lastskb_time.tv_sec,1000);
    CU_ASSERT_EQUAL(p_st_ct->st_connection_established.tv_sec,500);
    CU_ASSERT_PTR_NOT_NULL(p_st_ct->p_st_timer_entry);

    frfree(p_data);
    frfree(p_src_mac);
    frfree(p_dst_mac);
    frct_destroy();

    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t_frct_add 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", __case1);
  	CU_add_test(pSuite, "case2", __case2);
   	CU_add_test(pSuite, "case3", __case3);
   	CU_add_test(pSuite, "case4", __case4);
   	CU_add_test(pSuite, "case5", __case5);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}